part of 'win.dart';

BleDevice bleDeviceFromJson(String str) => BleDevice.fromJson(json.decode(str));

String bleDeviceToJson(BleDevice data) => json.encode(data.toJson());

class BleDevice {
  BleDevice(
      {required this.address, required this.rssi, required this.timestamp, required this.advType, required this.name, required this.serviceUuids, required this.manufacturerData, this.adStructures});

  String address;
  String name;
  String rssi;
  String timestamp;
  String advType;
  Uint8List manufacturerData;
  List<String> serviceUuids;
  List<AdStructure>? adStructures;

  factory BleDevice.fromJson(Map<String, dynamic> json) => BleDevice(
        address: json["deviceId"] ?? "",
        rssi: json["rssi"]?.toString() ?? "",
        timestamp: json["timestamp"]?.toString() ?? "",
        advType: json["advType"] ?? "",
        name: json["name"] ?? "",
        serviceUuids: json["serviceUuids"]?.cast<String>() ?? [],
        manufacturerData: json["manufacturerData"],
        adStructures: json["adStructures"] == null ? null : List<AdStructure>.from(json["adStructures"].map((x) => AdStructure(type: x["type"], data: List<int>.from(x["data"])))),
      );

  Map<String, dynamic> toJson() => {
        "deviceId": address,
        "rssi": rssi,
        "timestamp": timestamp,
        "advType": advType,
        "name": name,
        "serviceUuids": serviceUuids.toString(),
        "manufacturerData": manufacturerData.toString(),
      };
}

class AdStructure {
  int type;
  List<int> data;

  AdStructure({required this.type, required this.data});
}

class BaseChannelData {
  bool success;
  String message;

  BaseChannelData.fromJson(Map json)
      : success = json['success'] ?? false,
        message = json['message'] ?? "";
}

class BaseCharacteristicData extends BaseChannelData {
  DeviceIdentifier deviceId;
  Guid serviceUuid;
  Guid characteristicUuid;

  BaseCharacteristicData.fromJson(Map json)
      : deviceId = DeviceIdentifier(json['deviceId']),
        serviceUuid = Guid(json['service']),
        characteristicUuid = Guid(json['characteristic']),
        super.fromJson(json);
}

class OnValueChangeData extends BaseCharacteristicData {
  ValueChangeType type;
  Uint8List value;

  OnValueChangeData.fromJson(Map json, this.type)
      : value = json['data'] ?? [],
        super.fromJson(json);
}

enum ValueChangeType {
  write,
  read,
  notify,
}

class OnSetNotifyResult extends BaseCharacteristicData {
  final BleInputProperty property;

  OnSetNotifyResult.fromJson(Map json)
      : property = BleInputProperty.fromString(json['data'] ?? ""),
        super.fromJson(json);
}

class OnDiscoverServicesMessage extends BaseChannelData {
  DeviceIdentifier deviceId;
  List<BluetoothServiceWindows> service;

  OnDiscoverServicesMessage.fromJson(Map json)
      : deviceId = DeviceIdentifier(json['deviceId']),
        service = BluetoothServiceWindows.fromJson(DeviceIdentifier(json['deviceId']), json['data']?.cast<String, dynamic>() ?? {}),
        super.fromJson(json);

  @override
  String toString() {
    return 'OnDiscoverServicesMessage{success: $success, message: $message,deviceId: $deviceId, service: $service}';
  }
}

class OnMtuConfigMessage extends BaseChannelData {
  DeviceIdentifier deviceId;
  int mtu;

  OnMtuConfigMessage.fromJson(Map json)
      : deviceId = DeviceIdentifier(json['deviceId']),
        mtu = json['data'] ?? 23,
        super.fromJson(json);
}

class OnConnectionStateMessage extends BaseChannelData {
  DeviceIdentifier deviceId;
  BluetoothConnectionState state;

  OnConnectionStateMessage.fromJson(Map json)
      : deviceId = DeviceIdentifier(json['deviceId']),
        state = json['data'] == "connected" ? BluetoothConnectionState.connected : BluetoothConnectionState.disconnected,
        super.fromJson(json);
}

class OnAdapterStateChangedMessage extends BaseChannelData {
  BluetoothAdapterState state;

  static BluetoothAdapterState _getStateFromStr(String str) {
    if (str == "on") return BluetoothAdapterState.on;
    if (str == "off") return BluetoothAdapterState.off;
    if (str == "unknown") return BluetoothAdapterState.unknown;
    if (str == "disabled") return BluetoothAdapterState.unavailable;
    return BluetoothAdapterState.unknown;
  }

  OnAdapterStateChangedMessage.fromJson(Map json)
      : state = _getStateFromStr(json['data']),
        super.fromJson(json);
}

class OnTurnBlueMessage extends BaseChannelData {
  //操作的命令 on/off
  bool turnIsOn;

  //当前 结果开还是关
  bool isOn;

  OnTurnBlueMessage.fromJson(Map json)
      : turnIsOn = json['turnIsOn'],
        isOn = json['data'],
        super.fromJson(json);
}
